package com.liucxu.jiandanl.Business;

import com.google.gson.Gson;
import com.liucxu.jiandanl.Consts.Urls;
import com.liucxu.jiandanl.Entity.FreshNewsDetail;
import com.liucxu.jiandanl.Http.RequestManager;
import com.liucxu.jiandanl.Interface.RequestCallbackInterface;

import org.json.JSONException;
import org.json.JSONObject;

import de.greenrobot.event.EventBus;


/**
 * 获取新鲜详情的Event
 * Created by liuchenxu on 2016/8/4.
 */
public class FreshNewsDetailEvent extends BaseEvent {

    public FreshNewsDetail freshNewsDetail;

    /**
     * 获取新鲜事详情的方法
     *
     * @param id
     */
    public static void GetRefreshNewsDetail(int id) {

        RequestManager.GET(Urls.FRESH_NEWS_DETAIL_URL + id, new RequestCallbackInterface() {
            @Override
            public void onSuccess(String response) {
                try {
                    JSONObject dataObject = new JSONObject(response);
                    JSONObject postObject = dataObject.getJSONObject("post");
                    FreshNewsDetail mFreshNewsDetail = (new Gson()).fromJson(postObject.toString(), FreshNewsDetail.class);
                    FreshNewsDetailEvent event = new FreshNewsDetailEvent();
                    event.freshNewsDetail = mFreshNewsDetail;
                    event.setResultCode(1);
                    EventBus.getDefault().post(event);
                } catch (JSONException e) {
                    FreshNewsDetailEvent event = new FreshNewsDetailEvent();
                    event.setResultCode(0);
                    EventBus.getDefault().post(event);
                    e.printStackTrace();
                }
            }

            @Override
            public void onFail() {
                FreshNewsDetailEvent event = new FreshNewsDetailEvent();
                event.setResultCode(0);
                EventBus.getDefault().post(event);
            }
        });
    }
}
